import { isMobile } from './common'

// 设置rem
const setRem = () => {
  const isM = isMobile()
  if (isM) {
    setHtmlFontSize(750)
  } else {
    setHtmlFontSize(1920)
  }
}

// 设置html字体大小
const setHtmlFontSize = (width) => {
  ;(function (e) {
    let t
    let n = document
    let i = window
    let o = n.documentElement
    function u() {
      let t = (o.getBoundingClientRect().width / e) * 10
      o.style.fontSize = t + 'px'
    }
    u()
    i.addEventListener('resize', function () {
      clearTimeout(t)
      t = setTimeout(u, 300)
    })
    i.addEventListener('pageshow', function (e) {
      if (e.persisted) {
        clearTimeout(t)
        t = setTimeout(u, 300)
      }
    })
  })(width)
}

const rem2px = (rem) => {
  let w = document.documentElement.getBoundingClientRect().width
  return (rem * 750) / w
}

export { setRem, rem2px }
